$(function() {
    var grid = $('#gridSearch').datagrid({
        title: '业务信息查询',
        iconCls: 'icon-save',
        methord: 'post',
        url: '/Main/GetSearchList/',
        sortName: 'ID',
        sortOrder: 'desc',
        idField: 'ID',
        pageSize: 20,
        striped: true, //奇偶行是否区分
		rowStyler: function(index,row){
					if (row.OwingMoney<0 && !row.Paidup && row.Verifier!=null && row.Verifier != ""){
						return 'background-color:#EE3B3B;color:#fff;';
					}
				},
        frozenColumns: [[
	                { field: 'ck', checkbox: true }
				]],
        columns: [[
					{ field: 'BusinessCode', title: '编号' },
                    { field: 'TheName', title: '客户名称',sortable:true},
                    { field: 'Phone', title: '电话'},
                    { field: 'Contact', title: '联系人'},
                    { field: 'TypeName', title: '项目名称'},
					{ field: 'Charges', title: '合同金额'},
					{ field: 'BeginDate', title: '合同开始日期', formatter: $.getJsonDate},
					{ field: 'EndDate', title: '合同终止日期', formatter: $.getJsonDate},
					{ field: 'Introducer', title: '客户介绍人',sortable:true},
					{ field: 'Agent', title: '项目经办人',sortable:true},
					{ field: 'OfficeName', title:'所属部室'},
					{ field: 'DepartmentName', title: '所属分局'},
                    { field: 'OwingMoney', title: '欠费',sortable:true, formatter: function (value, row, index) {
                        if(row.Verifier!=null && row.Verifier != "")
						{
						    if (value>=0) {
                                return "否";
					        }
							else
								return value;
						}
						else
							return "";
					    }
					},
					{ field: 'BillDate', title: '开票日期', formatter: $.getJsonDate},
					{ field: 'BillUnitName', title: '开票单位'},
					{ field: 'ChargingDate', title: '交款日期', formatter: $.getJsonDate},
					{ field: 'FillIn', title:'填表人'},
					{ field: 'Verifier', title:'审核'},
					{ field: 'PaymentType', title:'类型',sortable:true},
					{ field: 'Paidup', title:'缴清', hidden: true}

				]],
        fit: true,
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        singleSelect: false,
        onDblClickRow: function (rowIndex, rowData) {  //双击事件
            $('<div></div>').dialog({
				title: "业务详情",
				modal: true,
				resizable: true,
				width: 900,
				height: 500,
				href: "/Main/ViewDetail?id="+rowData.ID,
				onClose:function(){  
                    $(this).dialog('destroy');
				},
				onLoad: function(){
					$("#viewForm").form('clear').form({
						'onLoadSuccess':function(data){
						var a = $("#OwingMoney").textbox('getText');
						if(a==""){
							$("#OwingMoney").textbox('setValue','0');  
						}
						$("#viewForm").find("#FoundDate").textbox('setValue',$.getJsonDate(data.FoundDate));
						$("#viewForm").find("#BeginDate").textbox('setValue',$.getJsonDate(data.BeginDate));
						$("#viewForm").find("#EndDate").textbox('setValue',$.getJsonDate(data.EndDate));
						$("#viewForm").find("#BillDate").textbox('setValue',$.getJsonDate(data.BillDate));
						$("#viewForm").find("#ChargingDate").textbox("setValue",$.getJsonDate(data.ChargingDate));
					}
					}).form('load', rowData);
				},
				onLoadError: function(data, status, error){
					if(data.status == "400")
						indexPage.reLogin();
				}
			});
        },
		onLoadError: function(data, status, error){
			if(data.status == "400")
				indexPage.reLogin();
		}
    });
    $('body').layout();
});

var BusinessSearch = {};
BusinessSearch.DataGridSearch = function(){
	var type1 = $("#ParentType").combobox('getValue');
	var type2 = $("#ChildrenType").combobox('getValue');
	var theOffice = "";
	if($("#TheOffice").length > 0){
		theOffice = $("#TheOffice").combobox('getValue');
	}
	var BillDepartment = $("#BillDepartment").combobox('getValue');
	var customerName = $("#searchName").val();
	var beginDate = $("#Date1").datebox('getValue');
	var endDate = $("#Date2").datebox('getValue');
	var isOverTime = $("#isOverTime").is(':checked');
	var isOwingMoney = $("#isOwingMoney").is(':checked');
	$("#gridSearch").datagrid({ url: '/Main/GetSearchList', queryParams: { 
	    type1: type1,
		type2: type2,
		theOffice: theOffice,
		BillDepartment: BillDepartment,
		customerName: customerName,
		beginDate: beginDate,
		endDate: endDate,
		isOverTime: isOverTime,
		isOwingMoney: isOwingMoney
	} });
}

BusinessSearch.DataGridToExcel = function(){
	var type1 = $("#ParentType").combobox('getValue');
	var type2 = $("#ChildrenType").combobox('getValue');
	var theOffice = "";
	if($("#TheOffice").length > 0){
		theOffice = $("#TheOffice").combobox('getValue');
	}
	var BillDepartment = $("#BillDepartment").combobox('getValue');
	var beginDate = $("#Date1").datebox('getValue');
	var endDate = $("#Date2").datebox('getValue');
	var isOverTime = $("#isOverTime").is(':checked');
	var isOwingMoney = $("#isOwingMoney").is(':checked');
	var a = document.getElementById("BusinessSearchExportToExcel");
    var url = "/main/DataExportToExcel?pageIndex=0";
    url += "&type1=" + type1;
    url += "&type2=" + type2;
	url += "&theOffice=" + theOffice,
	url += "&BillDepartment=" + BillDepartment,
	url += "&beginDate=" + beginDate,
	url += "&endDate=" + endDate,
	url += "&isOverTime=" + isOverTime,
	url += "&isOwingMoney=" + isOwingMoney
    a.href = url;
    a.click();
}